package pearson.automation;

import java.util.ArrayList;
import java.util.Calendar;

import pearson.automation.api.LibraryAPI;
import pearson.automation.api.SearchAPI;
import pearson.automation.assets.AssetWithRollOver;
import pearson.automation.forms.BrowseForm;
import pearson.automation.forms.StartForm;
import pearson.automation.forms.SearchResultsForm.AssetCategory;
import pearson.automation.forms.courseDetails.CollaboratorsForm;
import pearson.automation.forms.courseDetails.CourseDetailsForm;
import pearson.automation.forms.courseDetails.CourseForm;
import pearson.automation.forms.courseDetails.CourseSettings;
import pearson.automation.forms.courseDetails.AddCollaborators.AccessRights;

/**
 * Precondition class for permissions. Extended for RO and RW permissions.
 */
public class BasePearsonTestWithPermissionPreconditions 
                                    extends BasePearsonTestWithCourseCreation {

    protected String collaboratorRwName
                            = userData.getProperty("collaboratorRWname");
    protected String collaboratorRoName
                            = userData.getProperty("collaboratorROname");
    protected String[] lockedModules = new String[] {
                            "01", "03", "05", "07" };
    protected String[] unlockedModules = new String[] {
                            "02", "04", "06", "08" };
    protected static String courseName;
    protected static String courseEdition = "1";
    protected static String coursePrivacy = "Public";
    protected static String courseURL;
    protected String courseNamePrefix = "Collaboration Autotest";

    // Used in child classes
    public void addPermissions() {
        //TODO: Use pure API here
    logStep("Adding permissions");
    // Locking modules
        browser.getDriver().navigate().to(
                BasePearsonTestWithCourseCreation.courseURL);
        CourseForm courseForm = new CourseForm();
        CourseSettings settings = courseForm.clickSettings();
        CollaboratorsForm collaborators = settings.clickCollaborators();
        collaborators.clickMakeAllPublic();
        collaborators.lockModules(lockedModules);

    // Adding permissions for users
        collaborators.addCollaborator(collaboratorRwName, AccessRights.RW);
        collaborators.addCollaborator(collaboratorRoName, AccessRights.RO);
        collaborators.clickClose();

    // Set courseName
        CourseSettings courseSettings = courseForm.clickSettings();
        CourseDetailsForm details = courseSettings.clickCourseDetails();
        courseName = String.format(
                "%2$s %1$tT", Calendar.getInstance(), courseNamePrefix);
        details.setTitle(courseName);
        details.clickSave().waitForMessageDissapearing();
    }

    public void addAssets(
                String userId, String userLogin, String userPassword) {
        //TODO: Use pure API here
    logStep("Adding assets");
    // Adding assets in a library for users
        LibraryAPI api = new LibraryAPI();
        SearchAPI search = new SearchAPI();
    
        ArrayList<String> results = null;
        try {
            results = search.simpleSearch(
                    "", AssetCategory.IMAGES.toAPIString(), "5", "1");
            for (int i = 0; i < 5; i++) {
            api.addToLibrary(userId, results.get(i));
            }
        } catch (IndexOutOfBoundsException e) {
            fatal("Cannot add assets for user using API");
        }
        clearCacheForUser(userLogin, userPassword);
    }

    public void clearCacheForUser(String userName, String passwordValue) {
    // WORKAROUND FOR BROKEN API: drag first asset to My Library
        browser.exit();
        browser.openStartPage();
        new StartForm().goToLogin().login(userName, passwordValue);
        BrowseForm mainForm = new BrowseForm();
        mainForm.declareBrowseLists();
        AssetWithRollOver asset = mainForm.popularItems.getAsset(1);
        asset.dragToElement(mainForm.getMyLibraryElement());
        browser.waitForPageToLoad();
        int counter = 0;
        while (asset.canBeAddedToMyLibrary() && counter++ < 30) {
            try {
            Thread.sleep(500);
            } catch (InterruptedException e) {
            e.printStackTrace();
            }
        }
        asset.clickRemoveFromLibraryButtonFromRollOver();
    }
}
